<?php

namespace app\api\controller;

use app\api\model\Admin;
use app\api\model\Classes;
use app\api\model\Friends;
use app\api\model\Leave;
use app\api\model\Parents as ParentsModel;
use app\api\model\ParentStudents;
use app\api\model\StudentImages;
use app\api\model\Students;
use app\api\model\TeacherClasses;
use app\api\model\Teachers;
use app\api\model\Users;
use app\common\controller\Api;
use think\Controller;
use think\Request;

class Teacher extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 教师端登录
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (empty($account) || empty($password)) {
            $this->error('手机号和密码都不能为空');
        }
        $teacher = Teachers::getByAccount($account);
        if (!$teacher) {
            $this->error('账号不存在，请联系管理员分配账号');
        }
        if ($teacher->password != md5(sha1($password))) {
            $this->error('密码不正确，请重新输入');
        }
        $this->success('请求成功', compact('teacher'));
    }

    /**
     * 学生列表
     */
    public function student($teacher_id)
    {
        $class_id = $this->request->get('class_id');
        $student_name = $this->request->get('student_name');
        $length = $this->request->get('length');
        if ($class_id) {
            $classIds = TeacherClasses::getClassIds($teacher_id);
            if (!in_array($class_id, $classIds)) {
                $this->error('不是该老师的班级，请重新输入');
            }
        }
        $students = Students::getStudentsByTeacher($teacher_id, $class_id, $student_name, $length);
        $students->items() ? $this->success('请求成功', ['students' => $students->items()]) : $this->error("暂无数据", [], 2);
    }

    /**
     * 学生亲友团
     */
    public function studentFriend($student_id)
    {
        $friends = Friends::getFriendsBystudent($student_id);
        $friends ? $this->success('请求成功', compact('friends')) : $this->error("暂无数据", [], 2);
    }

    /**
     * 学生详情
     */
    public function studentDetail($student_id)
    {
        $student = Students::with(['classes', 'parents'])->find($student_id);
        if (!$student) {
            $this->error("学生不存在");
        }
        $parent_ids = array_column($student['parents'], 'id');
        $count = Friends::getFriendCounts($parent_ids);
        $student['friend_count'] = $count;
        $this->success('请求成功', compact('student'));
    }

    /**
     * 学生图片
     */
    public function addStudentPhoto($student_id, StudentImages $studentImages)
    {
        $images = $this->request->post('images');
        if (!$images) {
            $this->error('图片不能为空');
        }
        $res = $studentImages->addImages($student_id, $images);
        $res ? $this->success('请求成功') : $this->error("上传失败");
    }

    /**
     * 删除学生图片
     */
    public function delPhoto($photo_id)
    {
        $image = StudentImages::find($photo_id);
        if (!$image) {
            $this->error('图片不存在');
        }
        $res = $image->delete();
        $res ? $this->success('请求成功') : $this->error('删除失败');
    }

    /**
     * 二维码页面
     */
    public function qrcode()
    {
        $user_id = $this->request->post('user_id');
        $student_id = $this->request->post('student_id/a');
        if (empty($student_id) || empty($student_id)) {
            $this->error('用户和学生id都不能为空');
        }
        //家长还是亲友
        $user = Users::find($user_id);
        if (!$user) {
            $this->error('用户不存在');
        }
        $data = [];
        $parent = ParentsModel::getByPhone($user->phone);
        if ($parent) {
            unset($parent['parent_student']);
            $data = $parent;
        } else {
            $friend = Friends::getFriendByUser($user->phone);
            if (!$friend) {
                $this->error('用户不存在');
            }
            $data = $friend;
        }
        $data['image'] = $user->head;
        $students = Students::getStudentsByIds($student_id);
        $this->success('请求成功', ['user' => $data, 'students' => $students]);
    }

    /**
     * 宝贝姓名
     */
    public function name($parent_id)
    {
        $parent = ParentsModel::find($parent_id);
        if (!$parent) {
            $this->error('家长不存在');
        }
        $student_id = ParentStudents::getStudentIds($parent_id);
        if (!$student_id) {
            $this->error('学生不存在，请手动输入');
        }
        $students = Students::getStudentsByIds($student_id);
        if (!$students) {
            $this->error('学生不存在，请手动输入');
        }
        $class_id = array_column($students, 'class_id');
        $teacher_id = TeacherClasses::getTeacherIds($class_id);
        $teachers = Teachers::whereIn('id', $teacher_id)->field(['id', 'username'])->select();
        if (!$teachers) {
            $this->error('老师不存在');
        }
        $username = array_column($students, 'username');
        $this->success('请求成功', ['username' => $username, 'teachers' => $teachers]);
    }

    /**
     * 宝贝请假
     */
    public function leave($parent_id)
    {
        $username = $this->request->post('username');
        $reason = $this->request->post('reason');
        $teacher_id = $this->request->post('teacher_id');
        if (empty($username) || empty($reason) || !$teacher_id) {
            $this->error('姓名、原因和老师ID都不能为空');
        }
        $result = Leave::create([
            'parent_id' => $parent_id,
            'teacher_id' => $teacher_id,
            'username' => $username,
            'reason' => $reason
        ]);
        $result ? $this->success('请求成功') : $this->error('请求失败，请联系管理员');
    }

    /**
     * 老师审批
     */
    public function checkLeave($leave_id)
    {
        $type = $this->request->post('type');
        if (!$type) {
            $this->error('类型不能为空');
        }
        $leave = Leave::find($leave_id);
        if (!$leave) {
            $this->error('请假不存在');
        }
        switch ($type) {
            //拒绝
            case 1:
                $leave->status = 0;
                break;
            //同意
            case 2:
                $leave->status = 2;
                break;
            default:
                $this->error('类型错误');
                break;
        }
        $result = $leave->save();
        $result ? $this->success('请求成功') : $this->error('审核失败，请联系管理员');
    }

    /**
     * 老师审批列表
     */
    public function teacherApprove($teacher_id)
    {
        $length = $this->request->get('length');
        $leaves = Leave::getLeavesByTeacher($teacher_id, $length);
        $leaves->items() ? $this->success('请求成功', ['leaves' => $leaves->items()]) : $this->error('没有数据', [], 2);
    }


}
